package com.mrd.serverconnect.util;

/**
 * Created by cww on 2020/9/17.
 */

import android.content.Context;
import android.content.SharedPreferences;
import android.provider.Settings;
import android.util.Log;

/**
 * 设置WIFI在锁屏后一直保持连接
 * Created by suyxin on 2016/9/1.
 */
public class WifiUtils {

    private static final String WIFI_SLEEP_POLICY = "wifi_sleep_policy_value";
    public static void setWifiDormancy(Context context){
        int value = Settings.System.getInt(context.getContentResolver(), Settings.System.WIFI_SLEEP_POLICY,  Settings.System.WIFI_SLEEP_POLICY_DEFAULT);
        Log.d(WifiUtils.class.getSimpleName(), "setWifiDormancy() returned: " + value);
        final SharedPreferences prefs = context.getSharedPreferences("wifi_sleep_policy", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(WIFI_SLEEP_POLICY, value);
        editor.commit();
        if(Settings.System.WIFI_SLEEP_POLICY_NEVER != value){
            Settings.System.putInt(context.getContentResolver(), Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_NEVER);
        }
    }

    public static void restoreWifiDormancy(Context context){
        final SharedPreferences prefs = context.getSharedPreferences("wifi_sleep_policy", Context.MODE_PRIVATE);
        int defaultPolicy = prefs.getInt(WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_DEFAULT);
        Settings.System.putInt(context.getContentResolver(), Settings.System.WIFI_SLEEP_POLICY, defaultPolicy);
    }
}
